package com.koron.css2.serviceManage.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.css2.ServerInterface;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserFieldListBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.mapper.UserFieldConfigMapper;
import com.koron.css2.serviceManage.queryBean.UserFieldConfigQueryBean;
import com.koron.util.Constant;
import com.koron.util.JsonUtils;
import com.koron.util.Tools;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;
import java.util.Map;
/**
 * 用戶字段配置列表
 * @author 甄玉晴
 *
 */
public class UserFieldConfigList  implements ServerInterface{
	
	private static Logger logger = LogManager.getLogger(UserFieldConfigList.class);

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		
		@SuppressWarnings("rawtypes")
		MessageBean<PageInfo> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", PageInfo.class);
		try {	
			UserFieldConfigQueryBean bean = JsonUtils.objectToPojo(req.getData(), UserFieldConfigQueryBean.class);
			PageHelper.startPage(bean.getPage(), bean.getPageCount());
			//获取mapper
			UserFieldConfigMapper mapper = factory.getMapper(UserFieldConfigMapper.class);
			List<UserFieldListBean> list=mapper.userFieldConfigList(bean);
			//获取页面名称数据字典
			Map<String, String> pageNames = Tools.mapDicByCode(factory, "YMMC");
			for(UserFieldListBean field:list) {
				if(field.getStatus().equals("1")) {
					field.setStatus("启用");
				}if(field.getStatus().equals("0")){
					field.setStatus("停用");
				}if(field.getEditable().equals("1")) {
					field.setEditable("是");
				}if(field.getEditable().equals("0")){
					field.setEditable("否");
				}if(field.getMandatory().equals("1")) {
					field.setMandatory("是");
				}if(field.getMandatory().equals("0")){
					field.setMandatory("否");
				}
				if(!StringUtils.isEmpty(field.getPageName())) {
					field.setPageName(pageNames.get(field.getPageName()));;
				}
			}
			info.setData(new PageInfo<UserFieldListBean>(list));
		} catch (Exception e) {
			info.setCode(Constant.MESSAGE_INT_FAIL);
			info.setDescription(e.getMessage());
			logger.error(e.getMessage(), e);
		}
		return info;
	}


}
